package com.rc.saas.mini.service.car;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.mini.common.constant.WebConstant;
import com.rc.saas.mini.mapper.car.TenantCarNumberMapper;
import com.rc.saas.mini.model.car.TenantCarNumber;
import com.rc.saas.mini.model.car.TenantCarNumberExample;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.rc.RcMemberFlag;
import com.rc.saas.mini.service.member.MemberInfoService;
import com.rc.saas.mini.service.rc.RcMemberFlagService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/12/22
 */
@Service
public class TenantCarNumberServiceImpl implements TenantCarNumberService {

    @Autowired
    private TenantCarNumberMapper tenantCarNumberMapper;

    @Autowired
    private MemberInfoService memberInfoService;
    @Autowired
    private RcMemberFlagService rcMemberFlagService;

    @Override
    public TenantCarNumberMapper getMapper() {
        return tenantCarNumberMapper;
    }

    @Override
    public List<String> listCarNum(MemberInfo member) {
        List<String> list = tenantCarNumberMapper.listCarNum(member.getMemberId());
        return list;
    }

    @Override
    public TenantCarNumber selectDefaultCarNumber(Long memberId) {
        TenantCarNumberExample example = new TenantCarNumberExample();
        example.createCriteria().andMemberIdEqualTo(memberId).andIsDefaultEqualTo("1");
        List<TenantCarNumber> tenantCarNumbers = tenantCarNumberMapper.selectByExample(example);
        if (tenantCarNumbers.size() > 0) {
            return tenantCarNumbers.get(0);
        } else {
            return null;
        }
    }

    @Override
    public PageInfo<TenantCarNumber> listPageCarNum(MemberInfo member, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        TenantCarNumberExample example = new TenantCarNumberExample();
        example.createCriteria().andTenantCodeEqualTo(member.getTenantCode())
                .andMemberIdEqualTo(member.getMemberId());
        List<TenantCarNumber> tenantCarNumbers = tenantCarNumberMapper.selectByExample(example);
        return new PageInfo<>(tenantCarNumbers);
    }

    @Override
    public void saveOrUpdateCarNumber(MemberInfo memberInfo, TenantCarNumber carNumber) {




        if(WebConstant.CON1.equals(carNumber.getIsDefault())){
            //修改以前的默认数据
            TenantCarNumberExample example = new TenantCarNumberExample();
            example.createCriteria().andIsDefaultEqualTo(WebConstant.CON1).andMemberIdEqualTo(memberInfo.getMemberId());
            List<TenantCarNumber> list = tenantCarNumberMapper.selectByExample(example);
            if (list.size() > 0) {
                for (TenantCarNumber tenantCarNumber : list) {
                    tenantCarNumber.setIsDefault(WebConstant.CON0);
                    tenantCarNumberMapper.updateByPrimaryKeySelective(tenantCarNumber);
                }
            }
        }
        if (carNumber.getIsEnterprise() != null && WebConstant.CON1.equals(carNumber.getIsEnterprise())) {
            RcMemberFlag rcMemberFlag = rcMemberFlagService.selectByPrimaryKey(memberInfo.getMemberId());
            carNumber.setEnterpriseMemberId(rcMemberFlag.getEnterpriseMemberId());
        }
        if (carNumber.getNumberId() != null) {
            carNumber.setLastUpdatedTime(new Date());
            tenantCarNumberMapper.updateByPrimaryKeySelective(carNumber);
        } else {
            //目前的用户默认车牌号
            String currCarNum = memberInfo.getCarNumber();
            //如果用户没有默认车牌号，则将车牌号设置为默认车牌号
            if (currCarNum == null || StringUtils.isBlank(currCarNum)) {
                carNumber.setIsDefault(WebConstant.CON1);
            }
            carNumber.setTenantCode(memberInfo.getTenantCode());
            carNumber.setMemberId(memberInfo.getMemberId());
            carNumber.setCreatedTime(new Date());
            tenantCarNumberMapper.insertSelective(carNumber);
        }
        //是默认车牌号就同步到会员本身的车牌号
        if (WebConstant.CON1.equals(carNumber.getIsDefault())) {
            memberInfo.setCarArea(carNumber.getCarArea());
            memberInfo.setCarLetter(carNumber.getCarLetter());
            memberInfo.setCarNumber(carNumber.getCarNum());
            memberInfo.setCarType(carNumber.getCarType());
            memberInfo.setLastUpdatedTime(new Date());
            memberInfoService.updateByPrimaryKeySelective(memberInfo);
        }
    }

    @Override
    public void updateDefault(MemberInfo member, Long numberId) {
        //当前表修改
        TenantCarNumber number = tenantCarNumberMapper.selectByPrimaryKey(numberId);

        //将原默认设为普通车牌号
        TenantCarNumberExample exampleVo = new TenantCarNumberExample();
        exampleVo.createCriteria().andMemberIdEqualTo(member.getMemberId()).andTenantCodeEqualTo(member.getTenantCode()).andIsDefaultEqualTo("1");
        TenantCarNumber tenantCarNumberVo = new TenantCarNumber();
        tenantCarNumberVo.setIsDefault("0");
        tenantCarNumberMapper.updateByExampleSelective(tenantCarNumberVo, exampleVo);
        //设置默认车牌号
        TenantCarNumberExample example = new TenantCarNumberExample();
        example.createCriteria().andMemberIdEqualTo(member.getMemberId()).andTenantCodeEqualTo(member.getTenantCode()).andNumberIdEqualTo(numberId);
        TenantCarNumber tenantCarNumber = new TenantCarNumber();
        tenantCarNumber.setIsDefault("1");
        tenantCarNumberMapper.updateByExampleSelective(tenantCarNumber, example);

        //会员表修改
        member.setCarArea(number.getCarArea());
        member.setCarLetter(number.getCarLetter());
        member.setCarNumber(number.getCarNum());
        member.setCarType(number.getCarType());
        memberInfoService.updateByPrimaryKeySelective(member);

    }
}
